// 
// Created By: minanqiang.Men 2021/05/26
// 
#include "cybertron/BuildVersion.hpp"
#include "cybertron/core/SimOneEnv.hpp"
#include "cybertron/core/Log.hpp"
#include "cybertron/core/UtilString.hpp"
#ifdef CYBERTRON_LINUX
#include <cstring>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#endif
CYBERTRON_BEGIN
SimOneEnv::SimOneEnv() {
	mIsCloudEnv = false;
	mbInit = false;
	mConfigCenterPort = 8080;
	mConfigCenterAddr = "127.0.0.1";
	mPodNodeName = "";
	mPodNodeIP = "";
	mPodName = "";
	mPodNameSpace = "";
	mPodIP = "";
	mPodServiceAccount = "";
	mK8SDevOpsAddr = "";
	mK8SDevOpsPort = "";
	mPodSelfDeleted = false;

	mSimOneTaskRunnerPoolServerInfo.SONServerAddr = "127.0.0.1";
	mSimOneTaskRunnerPoolServerInfo.SONServerAddrPort = 23790;
	mSimOneTaskRunnerPoolServerInfo.SONHttpServerAddrPort = 8000;
	mSimOneTaskRunnerPoolServerInfo.EnableSONServer = false;
	//mbEnableSONServer = false;
	Init();

}
SimOneEnv::~SimOneEnv() {
}
CybertronCoreAPI  bool SimOneEnv::Init(){
	if (mbInit) {
		return false;
	}
	mbInit = true;
	char* cfg_center_addr = getenv("CONFIG_CENTER_ADDR");
	if (cfg_center_addr) {
		mConfigCenterAddr = cfg_center_addr;
	}
	else {
		mConfigCenterAddr = "127.0.0.1";
	}

	char* cfg_center_port = getenv("CONFIG_CENTER_PORT");
	if (cfg_center_port) {
		mConfigCenterPort = atoi(cfg_center_port);
	}
	else {
		mConfigCenterPort = 8080;
	}

	char* pod_name = getenv("MY_POD_NAME");
	if (pod_name) {
		mPodName = pod_name;
		mIsCloudEnv = true;
	}
	else {
		mIsCloudEnv = false;
	}

	char* container_name = getenv("MY_CONTAINER_NAME");
	if (container_name) {
		mContainerName = container_name;
	}

	//

	char* pod_namespace = getenv("MY_POD_NAMESPACE");
	if (pod_namespace) {
		mPodNameSpace = pod_namespace;
	}

	char* api_server_addr = getenv("DEVOPS_API_SERVER_ADDR");
	if (api_server_addr) {
		mK8SDevOpsAddr = api_server_addr;
	}

	char* api_server_port = getenv("DEVOPS_API_SERVER_PORT");
	if (api_server_port) {
		mK8SDevOpsPort = api_server_port;
	}

	char* pod_ip = getenv("MY_POD_IP");
	if (pod_ip) {
		mPodIP = pod_ip;
	}

	char* pod_account = getenv("MY_POD_SERVICE_ACCOUNT");
	if (pod_account) {
		mPodServiceAccount = pod_account;
	}

	char* node_name = getenv("MY_NODE_NAME");
	if (node_name) {
		mPodNodeName = node_name;
	}

	char* node_ip = getenv("MY_NODE_IP");
	if (node_ip) {
		mPodNodeIP = node_ip;
	}

	char* self_deleted = getenv("CYBERTRON_ALL_IN_ONE_DEPLOY");
	if (self_deleted) {
		mPodSelfDeleted = atoi(self_deleted);
	}

	char* tag = getenv("WORKER_TAG");
	if (tag) {
		mPodTagString = tag;
		logInfo("SimOne Tag %s  ======>", mPodTagString.c_str());
		UtilString::split(mPodTagList, tag, ';');

	}

	char* cap = getenv("WORKER_CAP");
	if (cap) {
		mWorkCapString = cap;
		logInfo("SimOne Work Cap %s  ======>", mWorkCapString.c_str());
		UtilString::split(mWorkCapList, cap, ';');
	}

	char* work_num = getenv("CYBERTRON_WORKER_NUM");
	if (work_num) {
		mK8SPodWorkNum = atoi(work_num);
	}
	else {
		mK8SPodWorkNum = 0;
	}

	char* base_workId = getenv("WORK_ID_BASE");
	if (work_num) {
		logInfo("SimOne Work BASE ID %s  ======>", base_workId);
		mBaseWorkId = base_workId;
	}
	else {
		mBaseWorkId = "60001";
	}
	
	
	logInfo("SimOne ConfigCenter Server ======> %s:%d", mConfigCenterAddr.c_str(), mConfigCenterPort);
	logInfo("SimOne work num  ======> %d", mK8SPodWorkNum);

	mSimOneTaskRunnerPoolServerInfo.EnableSONServer = false;
	char* son_server_addr = getenv("SON_SERVER_ADDR");
	if (son_server_addr) {
		mSimOneTaskRunnerPoolServerInfo.EnableSONServer = true;
		mSimOneTaskRunnerPoolServerInfo.SONServerAddr = son_server_addr;
	}

	char* son_server_port = getenv("SON_SERVER_PORT");
	if (son_server_port) {

		 mSimOneTaskRunnerPoolServerInfo.SONServerAddrPort = atoi(son_server_port);
	}

	char* son_http_server_port = getenv("SON_HTTP_SERVER_PORT");
	if (son_http_server_port) {
		mSimOneTaskRunnerPoolServerInfo.SONHttpServerAddrPort = atoi(son_http_server_port);
	}
	
	logWarning("Task Pooling Server %s:tcp:%d ,http:%d,enable:%d", mSimOneTaskRunnerPoolServerInfo.SONServerAddr.c_str(), 
		mSimOneTaskRunnerPoolServerInfo.SONServerAddrPort, mSimOneTaskRunnerPoolServerInfo.SONHttpServerAddrPort, mSimOneTaskRunnerPoolServerInfo.EnableSONServer);
	if (!mIsCloudEnv)
	{
		logWarning("SimOne is not Cloud Environment");
	}
	else {
		logWarning("SimOne is Cloud Environment!!!!!");
		logInfo("Cloud Env Info Pod Name	======> %s",mPodName.c_str());
		logInfo("Cloud Env Info Pod NS======> %s", mPodNameSpace.c_str());
		logInfo("Cloud Env Info Pod SA======> %s", mPodServiceAccount.c_str());
		logInfo("Cloud Env Info Pod PodIP======> %s", mPodIP.c_str());
		logInfo("Cloud Env Info NodeName======> %s", mPodNodeName.c_str());
		logInfo("Cloud Env Info NodeIP======> %s", mPodNodeIP.c_str());
		logInfo("Cloud Env Info PodSelfDele======> %d", mPodSelfDeleted);
		logInfo("Cloud Env Info K8SAPIServerAddr======> %s", mK8SDevOpsAddr.c_str());
	
		for (size_t i = 0; i < mPodTagList.size(); i++)
		{
			logInfo("SimOne Env Tag ======>%s", mPodTagList[i].c_str());
		}
	}
	logInfo("Channel Name: ======> %s", CHANNEL_NAME);
	mbInit = true;
	return true;
}
CybertronCoreAPI  bool SimOneEnv::IsCloudEnv() {
	return mIsCloudEnv;
}
CybertronCoreAPI  int SimOneEnv::GetPodWorkNum() {
	return mK8SPodWorkNum;
}

CybertronCoreAPI  std::string SimOneEnv::GetChannelValue() {
	return CHANNEL_PWD;
}

CybertronCoreAPI  std::string SimOneEnv::GetProductType() {
	return PRODUCT_TYPE;
}

CybertronCoreAPI  std::string SimOneEnv::GetProductVersion() {
	return PRODUCT_VERSION;
}
CybertronCoreAPI  std::string SimOneEnv::GetProductInfo() {
	std::string productInfo = PRODUCT_TYPE;
	productInfo += "_";
	productInfo += PRODUCT_VERSION;
	productInfo += "_";
	productInfo += CHANNEL_NAME;
	productInfo += "_";
	productInfo += BRANCH_NAME;
	return productInfo;
}
CybertronCoreAPI  bool SimOneEnv::GetConfigCenterInfo(SimOneConfigCenterInfo &ConfigCenterInfo) {
	ConfigCenterInfo.ConfigCenterAddr = mConfigCenterAddr;
	ConfigCenterInfo.ConfigCenterPort = mConfigCenterPort;
	return true;
}
CybertronCoreAPI  bool SimOneEnv::GetCloundEnvInfo(SimOneCloudEnvInfo& CloundEnvInfo) {
	CloundEnvInfo.PodNodeName = mPodNodeName;
	CloundEnvInfo.PodNodeIP = mPodNodeIP;
	CloundEnvInfo.PodName = mPodName;
	CloundEnvInfo.PodNameSpace = mPodNameSpace;
	CloundEnvInfo.PodIP = mPodIP;
	CloundEnvInfo.PodServiceAccount = mPodServiceAccount;
	CloundEnvInfo.PodSelfDeleted = mPodSelfDeleted;
	CloundEnvInfo.ContainerName = mContainerName;
	return true;
}
CybertronCoreAPI  bool SimOneEnv::GetBaseWorkId(int& baseWorkId) {
	if (mBaseWorkId.size() <= 0) {
		baseWorkId = 60001;
		return false;
	}
	baseWorkId = atoi(mBaseWorkId.c_str());
	return true;
}
CybertronCoreAPI  bool SimOneEnv::GetSimOneK8SDevOpsInfo(SimOneK8SDevOpsInfo& CloundServerInfo) {
	CloundServerInfo.K8SDevOpsAddr = mK8SDevOpsAddr;
	CloundServerInfo.K8SDevOpsPort = mK8SDevOpsPort;
	return true;
}

CybertronCoreAPI  bool SimOneEnv::GetTaskRunnerPoolInfo(SimOneTaskRunnerPoolServerInfo& serverInfo) {
	/*serverInfo.SONServerAddr = mSONServerAddr;
	serverInfo.SONServerAddrPort = mSONServerPort;
	serverInfo.SONHttpServerAddrPort = mSONHttpServerPort;
	serverInfo.EnableSONServer = mbEnableSONServer;*/
	serverInfo = mSimOneTaskRunnerPoolServerInfo;
	return true;
}

CybertronCoreAPI  bool SimOneEnv::hasPodTag(std::string tag) {
	for (size_t i = 0; i < mPodTagList.size(); i++)
	{
		if (mPodTagList[i] == tag) {
			return true;
		}
	}
	return false;
}
CybertronCoreAPI  std::string SimOneEnv::GetPodTagString() {
	return mPodTagString;
}

CybertronCoreAPI  bool SimOneEnv::GetWorkCapList(std::vector<std::string>& capList) {

	capList = mWorkCapList;
	return true;
}
CYBERTRON_END